package ${package.ServiceImpl};

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.bean.BeanUtil;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fansl.koala.quickdev.common.bean.PageBean;
import com.fansl.koala.quickdev.common.constants.CommonConstants;
import ${package.Entity?replace(".entity",".vo")}.${entity}Vo;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
<#assign entityName='${entity?uncap_first}' />
/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
    /**
    * 分页查询数据
    *
    * @param page 分页条件
    * @param ${entityName}   查询参数
    * @return 分页数据
    */
    @Override
    public PageBean<${entity}Vo> listPage(PageBean<${entity}Vo> page, ${entity} ${entityName}){
        //分页参数
        Page<${entity}> queryPage = new Page<>();
        queryPage.setCurrent(page.getPageNo());
        queryPage.setSize(page.getPageSize());
        if (StrUtil.isNotBlank(page.getSortOrder()) && StrUtil.isNotBlank(page.getSortField())) {
            OrderItem orderItem = new OrderItem();
            orderItem.setAsc(page.getSortOrder().equals(CommonConstants.PAGE_QUERY_ORDER_ASC));
            orderItem.setColumn(StrUtil.toUnderlineCase(page.getSortField()));
            queryPage.setOrders(Collections.singletonList(orderItem));
        }
        //查询条件
        ${entity} queryParam = new ${entity}();
        LambdaQueryWrapper<${entity}> queryWrapper = Wrappers.lambdaQuery(queryParam);
        Page<${entity}> result = this.page(queryPage, queryWrapper);
        //结果转vo
        List<${entity}Vo> data = ${entityName}2Vo(result.getRecords());
         //转换结果
         page.setData(data);
         page.setTotalCount(result.getTotal());
         page.setTotalPage(result.getPages());
         return page;
    }

    /**
    * Vo对象转换
    * @param ${entityName}s 源对象
    * @return 目标对象
    */
    private List<${entity}Vo> ${entityName}2Vo(List<${entity}> ${entityName}s){
        if(${entityName}s.isEmpty()){
            return Collections.emptyList();
        }
       return ${entityName}s.stream().map(${entityName}->{
            ${entity}Vo target = new ${entity}Vo();
            BeanUtil.copyProperties(${entityName},target);
            return target;
        }).collect(Collectors.toList());
    }

    /**
    * Vo对象转换
    * @param ${entityName} 源对象
    * @return 目标对象
    */
    private ${entity}Vo ${entityName}2Vo(${entity} ${entityName}){
        ${entity}Vo target = new ${entity}Vo();
        BeanUtil.copyProperties(${entityName},target);
        return target;
    }
}
</#if>
